package com.v.im.businessservice.service.impl;
import com.google.common.collect.Lists;

import com.v.im.businessapi.dto.resultDTO.ImFriendGroupResultDTO;
import com.v.im.businessapi.dto.resultDTO.ImUserResultDTO;
import com.v.im.businessservice.bean.ImGroupEntity;
import com.v.im.businessservice.bean.ImUserEntity;
import com.v.im.businessservice.bean.ImUserFriendEntity;
import com.v.im.businessservice.service.base.BaseService;
import com.v.im.businessservice.service.iface.IImUserFriendService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ImUserFriendServiceImpl extends BaseService implements IImUserFriendService {

    @Override
    public List<ImFriendGroupResultDTO> getUserFriends(String userId) {
        List<ImFriendGroupResultDTO> resultDTOS = new ArrayList<>();
        List<ImUserFriendEntity> imUserFriendEntities = imUserFriendDao.findByUserId(userId);
        imUserFriendEntities.addAll(imUserFriendDao.findByFriendId(userId));
        if(CollectionUtils.isEmpty(imUserFriendEntities)){
            return resultDTOS;
        }
        //查询用户信息
        Set<String> userIds = imUserFriendEntities.stream().map(ImUserFriendEntity::getUserId).collect(Collectors.toSet());
        userIds.addAll(imUserFriendEntities.stream().map(ImUserFriendEntity::getFriendId).collect(Collectors.toSet()));
        List<ImUserEntity> imUserEntities = imUserDao.findByIdIn(userIds);
        Map<String, ImUserEntity> imUserEntityMap = imUserEntities.stream().collect(Collectors.toMap(ImUserEntity::getId, Function.identity()));


        //查询群组信息
        Set<String> groupIds = imUserFriendEntities.stream().map(ImUserFriendEntity::getUserGroupId).collect(Collectors.toSet());
        groupIds.addAll(imUserFriendEntities.stream().map(ImUserFriendEntity::getFriendGroupId).collect(Collectors.toSet()));
        List<ImGroupEntity> imGroupEntities = imGroupDao.findByIdIn(groupIds);
        Map<String, ImGroupEntity> imGroupEntityMap = imGroupEntities.stream().collect(Collectors.toMap(ImGroupEntity::getId, Function.identity()));


        for (ImUserFriendEntity imUserFriendEntity : imUserFriendEntities) {
            ImFriendGroupResultDTO imFriendGroupResultDTO = new ImFriendGroupResultDTO();
            String groupId = StringUtils.isBlank(imUserFriendEntity.getFriendGroupId()) ? imUserFriendEntity.getUserGroupId() : imUserFriendEntity.getFriendGroupId();
            ImGroupEntity imGroupEntity = imGroupEntityMap.get(groupId);
            imFriendGroupResultDTO.setId(imGroupEntity.getId());
            imFriendGroupResultDTO.setName(imGroupEntity.getName());

            String userIdStr = StringUtils.isBlank(imUserFriendEntity.getUserId()) ? imUserFriendEntity.getFriendId() : imUserFriendEntity.getUserId();
            imFriendGroupResultDTO.setUserList(Lists.newArrayList());

            ImUserEntity imUserEntity = imUserEntityMap.get(userIdStr);
            ImUserResultDTO imUserResultDTO = new ImUserResultDTO();
            BeanUtils.copyProperties(imUserEntity,imUserResultDTO);

            List<ImUserResultDTO> imUserEntitiesList = new ArrayList<>();
            imUserEntitiesList.add(imUserResultDTO);
            imFriendGroupResultDTO.setUserList(imUserEntitiesList);
            resultDTOS.add(imFriendGroupResultDTO);
        }
        return resultDTOS;
    }
}
